package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.entity.LbbDict;
import com.lebang.example.LbbDictExample;
import com.lebang.model.enums.Booleans;
import com.lebang.service.sys.DictService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: hwq
 * @Date: 2019/11/6 10:00
 * @Version 1.0
 */
@Controller
@RequestMapping("/sys/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    @Login
    @GetMapping("/list")
    public String list() {
        return "/dict/list";
    }
    @GetMapping("/add")
    public String add(Long id, Model model) {
        LbbDict vo;
        if (id != null) {
            vo = dictService.select(id);
        } else {
            vo = new LbbDict();
        }
        model.addAttribute("item", vo);
        return "/dict/edit";
    }
    @PostMapping("/list")
    @ResponseBody
    @Login
    public Map data(HttpServletRequest request, Integer page, Integer limit) {
        Map map = new HashMap(1);
        LbbDictExample example = new LbbDictExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        example.createCriteria().andHasDeletedEqualTo(Booleans.False.name());
        List<LbbDict> rewardsList = dictService.selectByExample(example);
        Long count = dictService.countByExample(example);
        map.put("count", count);
        map.put("code", 0);
        map.put("data", rewardsList);
        return map;
    }

    @Login
    @ResponseBody
    @PostMapping("/delete")
    public JsonData delete(HttpServletRequest request, Integer id) {
        dictService.delete(id);
        return Callback.success("success", "修改成功");
    }

    @ResponseBody
    @Login
    @PostMapping("/edit")
    public JsonData update(HttpServletRequest request, LbbDict dict) {
        dictService.updateByPrimaryKeySelective(dict);
        return Callback.success("success", "修改成功");
    }
    @PostMapping("/edit1")
    @ResponseBody
    public JsonData edit(LbbDict vo) {
        if (vo.getId() != null) {
            dictService.updateByPrimaryKeySelective(vo);
        } else {
            vo.setCreateTime(new Date());
            vo.setModifyTime(new Date());
            dictService.insertSelective(vo);
        }
        return Callback.success("success", "修改成功");
    }
}
